<template>
  <div class="model-container">
    <div class="listName">积分商品</div>
    <!--搜索-->
    <el-card shadow="never">
      <div slot="header" class="clearfix">
        <span>快捷搜索</span>
        <div class="btns-wrapper-right">
          <el-button v-if="!searchShow" icon="el-icon-arrow-down" @click="searchShow = true"/>
          <el-button v-else icon="el-icon-arrow-up" @click="searchShow = false"/>
        </div>
      </div>
      <el-collapse-transition>
        <div v-show="searchShow" class="cardbody">
          <el-form ref="seacherParam" :model="seacherParam">
            <el-row :gutter="10">
              <el-col :xl="5" :lg="6" :md="6" :sm="8" >
                <el-col :xl="7" :lg="7" :md="8" :sm="8" class="input-name">商品名称</el-col>
                <el-col :xl="17" :lg="17" :md="16" :sm="16">
                  <el-form-item prop="itemTitle">
                    <el-input v-model="seacherParam.itemTitle" size="small" placeholder="请输入商品名称"/>
                  </el-form-item>
                </el-col>
              </el-col>
              <el-col :xl="5" :lg="6" :md="6" :sm="8" >
                <el-col :xl="7" :lg="7" :md="8" :sm="8" class="input-name">商品编号</el-col>
                <el-col :xl="17" :lg="17" :md="16" :sm="16">
                  <el-form-item prop="sellerItemNo">
                    <el-input v-model="seacherParam.sellerItemNo" size="small" placeholder="请输入商品编号"/>
                  </el-form-item>
                </el-col>
              </el-col>
              <el-col :xl="5" :lg="6" :md="6" :sm="8" >
                <el-col :xl="7" :lg="7" :md="8" :sm="8" class="input-name">上下架状态</el-col>
                <el-col :xl="17" :lg="17" :md="16" :sm="16">
                  <el-form-item prop="listStatus">
                    <el-select v-model="seacherParam.listStatus" value="1" placeholder="请选择" size="small">
                      <el-option :value="0" label="待上架"/>
                      <el-option :value="1" label="已上架"/>
                      <el-option :value="2" label="已下架"/>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-col>
              <el-col :xl="5" :lg="6" :md="6" :sm="8" >
                <el-col :xl="7" :lg="7" :md="8" :sm="8" class="input-name">审核状态</el-col>
                <el-col :xl="17" :lg="17" :md="16" :sm="16">
                  <el-form-item prop="censorStatus">
                    <el-select v-model="seacherParam.censorStatus" value="1" placeholder="请选择" size="small">
                      <el-option :value="0" label="待审核"/>
                      <el-option :value="1" label="已审核"/>
                      <el-option :value="2" label="审核不通过"/>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-col>
            </el-row>
            <div class="cardfooter">
              <el-button type="primary" size="mini" plain @click="seachFun">查询</el-button>
              <el-button type="warning" size="mini" plain @click="resetFun">重置</el-button>
            </div>
          </el-form>
        </div>
      </el-collapse-transition>
    </el-card>
    <!--表格-->
    <el-card style="margin-top: 10px">
      <div slot="header" class="clearfix">
        <div class="btns-wrapper-left">
          <el-button type="primary" plain @click="add">新增</el-button>
          <el-button type="success" plain @click="soldTop">上架</el-button>
          <el-button type="success" plain @click="soldOut">下架</el-button>
          <el-button type="primary" plain @click="audit">审核</el-button>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table v-loading="!dataOption" :data="dataOption" stripe border empty-text="暂无数据" @selection-change="selectChange">

          <el-table-column align="center" type="selection" width="55"/>

          <el-table-column align="center" prop="itemTitle" label="标题"/>

          <el-table-column align="center" label="资讯封面图">
            <template slot-scope="scope">
              <img :src="scope.row.defaultPicUrl" width="50px" height="50px">
            </template>
          </el-table-column>

          <el-table-column align="center" prop="sellerItemNo" label="商品编号"/>

          <!--<el-table-column align="center" label="商品分类">-->
          <!--<template slot-scope="scope">{{ scope.row.itemCatId}}</template>-->
          <!--</el-table-column>-->

          <!--<el-table-column align="center" prop="brandName" label="商品品牌"/>-->

          <el-table-column align="center" prop="integralPrice" label="积分价"/>

          <el-table-column align="center" label="上下架状态">
            <template slot-scope="scope">{{ listStatusVal(scope.row.listStatus) }}</template>
          </el-table-column>

          <el-table-column align="center" prop="createTime" label="上架时间"/>

          <el-table-column align="center" label="是否新品">
            <template slot-scope="scope">{{ isNewVal(scope.row.isNew) }}</template>
          </el-table-column>

          <el-table-column align="center" label="是否热卖">
            <template slot-scope="scope">{{ isSellingHotVal(scope.row.isSellingHot) }}</template>
          </el-table-column>

          <el-table-column align="center" label="审核状态">
            <template slot-scope="scope">{{ censorStatusVal(scope.row.censorStatus) }}</template>
          </el-table-column>

          <el-table-column align="center" label="操作" width="200px">
            <template slot-scope="scope">

              <el-button title="查看" type="primary" circle class="table-icon " @click="check(scope.row)"><i class="iconfont icon-chakan2"/></el-button>

              <el-button
                v-if="scope.row.censorStatus === 0 ||
                  scope.row.censorStatus === 2 ||
                  scope.row.listStatus === 0 ||
                scope.row.listStatus === 2"
                title="编辑"
                type="primary"
                circle
                icon="el-icon-edit"
                class="table-icon"
                @click="edit(scope.row)"/>

              <el-button
                v-if="scope.row.censorStatus === 0 ||
                  scope.row.censorStatus === 2 ||
                  scope.row.listStatus === 0 ||
                  scope.row.listStatus === 2 &&
                scope.row.isDeleted === 0"
                title="回收站"
                type="warning"
                circle
                icon="el-icon-delete"
                class="table-icon"
                @click="del(scope.row)"/>

              <el-button v-if="scope.row.isDeleted === 1" title="恢复" type="primary" circle class="table-icon " @click="goodRestore(scope.row)"><i class="iconfont icon-huifu"/></el-button>

              <el-button
                v-if="scope.row.censorStatus === 0 ||
                  scope.row.censorStatus === 2 ||
                  scope.row.listStatus === 0 ||
                scope.row.listStatus === 2"
                title="关联商品"
                type="primary"
                circle
                class="table-icon "
                @click="associated(scope.row)"><i class="iconfont icon-Link"/></el-button>

            </template>
          </el-table-column>

        </el-table>

        <!--分页-->
        <el-pagination
          :current-page="pageNo"
          :page-sizes="[10, 20, 30]"
          :page-size="pageSize"
          :total="rowCount"
          background
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>

    <!--商品分类弹框-->
    <el-dialog
      :visible.sync="dialogShopState"
      :close-on-click-modal="false"
      top="5vh"
      width="30%"
      title="商品分类">
      <template v-if="dialogType === 1">
        <div style="padding: 0 20px 20px 20px">
          <el-input v-model="filterText" size="small" placeholder="输入关键字进行过滤" style="margin-bottom: 20px"/>
          <el-tree
            ref="tree"
            :data="goodClassList"
            :props="defaultProps"
            :filter-node-method="filterNode"
            class="filter-tree"
            show-checkbox
            highlight-current
            check-strictly
            node-key="catId"/>
        </div>
        <div class="btns-wrapper">
          <el-button size="small" @click="getCheckedNodes">确认</el-button>
          <el-button type="primary" size="small" @click="threeReset">重置</el-button>
        </div>
      </template>

      <template v-else-if="dialogType === 2">
        <el-form ref="dialogBindData" :model="dialogBindData" label-width="30%" status-icon class="demo-ruleForm">
          <div class="formBody">
            <el-form-item label="审核状态" prop="censorStatus">
              <el-radio-group v-model="dialogBindData.censorStatus">
                <el-radio :label="1">通过</el-radio>
                <el-radio :label="2">不通过</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="备注" prop="memo">
              <el-input v-model="dialogBindData.memo" autosize type="textarea" placeholder="备注"/>
            </el-form-item>
          </div>
          <div class="btns-wrapper">
            <el-button size="small" @click="shophandleClose">取 消</el-button>
            <el-button type="primary" size="small" @click="submmitFun">保 存</el-button>
          </div>
        </el-form>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import * as commodity from '../../api/commodity'
export default {
  data() {
    return {
      // 页面搜索
      seacherParam: {
        itemTitle: '',
        sellerItemNo: '',
        listStatus: '',
        censorStatus: ''
      },
      searchShow: false,
      pageNo: 1,
      pageSize: 10,
      rowCount: null, // 总条数
      searchStatus: false,
      // 商品分类弹框
      dialogShopState: false,
      dialogType: 1,
      // 列表数据
      dataOption: null,
      selectData: null,
      // 提交内容
      dialogBindData: {
        censorStatus: 1,
        memo: ''
      },
      // 商品分类three树
      filterText: '',
      goodsClassText: '',
      goodClassList: null,
      defaultProps: {
        children: 'children',
        label: 'catName'
      }
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  beforeMount() {
    this.getList()
  },
  methods: {
    filterNode(value, data) {
      if (!value) return true
      return data.catName.indexOf(value) !== -1
    },
    seachFun() {
      if (!this.searchStatus) {
        this.pageNo = 1
      }
      const param = {
        current: this.pageNo,
        size: this.pageSize
      }
      const arr = []
      for (const key in this.seacherParam) {
        if (this.seacherParam[key] !== '') {
          arr.push('&' + key + '=' + this.seacherParam[key])
        }
      }
      const url = arr.join('')
      commodity.newsearchCommodityList(param, url).then(res => {
        if (res.data.code !== 1) {
          this.$message.error(res.data.msg)
          return false
        }
        if (res.data.data === 'null' || res.data.data === null) {
          return false
        }
        // 初始话表格数据
        this.dataOption = res.data.data.records
        // 初始化分页数据
        this.rowCount = res.data.data.total
        this.pageSize = res.data.data.size
        this.searchStatus = false
      })
    },
    getList(cb) {
      const param = {
        current: this.pageNo,
        size: this.pageSize,
        isDeleted: 0,
        itemType: 1
      }
      commodity.getCommodityList(param).then(res => {
        if (res.data.code !== 1) {
          this.$message.error(res.data.msg)
          return false
        }
        if (res.data.data === 'null' || res.data.data === null) {
          return false
        }
        // 初始话表格数据
        this.dataOption = res.data.data.records
        // 初始化分页数据
        this.rowCount = res.data.data.total
        this.pageSize = res.data.data.size
      })
    },
    // 添加
    add() {
      this.$router.push({
        name: '/v1/integralgoods_add',
        params: {
          type: 1
        }
      })
    },
    // 编辑
    edit(data) {
      this.$router.push({
        name: '/v1/integralgoods_add',
        params: {
          type: 2,
          data: data
        }
      })
    },
    // 查看
    check(data) {
      this.$router.push({
        name: '/v1/integralgoods_add',
        params: {
          type: 3,
          data: data
        }
      })
    },
    // 上架
    soldTop() {
      if (this.selectData === null || this.selectData === '' || this.selectData === undefined || this.selectData.length === 0) {
        this.$message({
          type: 'warning',
          message: '请选择商品'
        })
        return false
      }
      for (const val of this.selectData) {
        if (val.listStatus === 1 || val.censorStatus !== 1) {
          this.$message({
            type: 'warning',
            message: '注意！请选择【已审核,待上架,已下架】的商品'
          })
          return false
        }
      }
      this.$confirm('确认要上架商品吗？?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.soldTopData()
      }).catch(() => {
        console.log('已取消')
      })
    },
    soldTopData() {
      let itemID = []
      for (const data of this.selectData) {
        itemID.push(data.itemId)
      }
      itemID = itemID.join(',')
      const param = {
        listStatus: 1,
        itemId: itemID
      }
      commodity.upadownCommodityList(param).then(res => {
        if (res.data.code === 1) {
          this.$message({
            type: 'success',
            message: '商品已上架'
          })
          this.getList()
        }
      })
    },
    // 下架
    soldOut() {
      if (this.selectData === null || this.selectData === '' || this.selectData === undefined || this.selectData.length === 0) {
        this.$message({
          type: 'warning',
          message: '请选择商品'
        })
        return false
      }
      for (const val of this.selectData) {
        if (val.listStatus !== 1) {
          this.$message({
            type: 'warning',
            message: '注意！请选择【已上架】的商品'
          })
          return false
        }
      }
      this.$confirm('确认要下架商品吗？?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.soldOutData()
      }).catch(() => {
        console.log('已取消')
      })
    },
    soldOutData() {
      let itemID = []
      for (const data of this.selectData) {
        itemID.push(data.itemId)
      }
      itemID = itemID.join(',')
      const param = {
        listStatus: 2,
        itemId: itemID
      }
      commodity.upadownCommodityList(param).then(res => {
        if (res.data.code === 1) {
          this.$message({
            type: 'success',
            message: '商品已下架'
          })
          this.getList()
        }
      })
    },
    // 审核
    audit() {
      if (this.selectData === null || this.selectData === '' || this.selectData === undefined) {
        this.$message({
          type: 'warning',
          message: '请选择商品'
        })
        return false
      }
      for (const val of this.selectData) {
        if (val.censorStatus === 1 || val.censorStatus === 3) {
          this.$message({
            type: 'warning',
            message: '注意！请选择【待审核,审核不通过】的商品'
          })
          return false
        }
      }
      this.dialogType = 2
      this.dialogShopState = true
    },
    submmitFun() {
      let itemID = []
      for (const data of this.selectData) {
        itemID.push(data.itemId)
      }
      itemID = itemID.join(',')
      const param = {
        censorStatus: this.dialogBindData.censorStatus,
        memo: this.dialogBindData.memo,
        itemId: itemID
      }
      commodity.upadownCommodityList(param).then(res => {
        if (res.data.code === 1) {
          if (this.dialogBindData.censorStatus === 1) {
            this.$message({
              type: 'success',
              message: '审核通过'
            })
          } else if (this.dialogBindData.censorStatus === 2) {
            this.$message({
              type: 'warning',
              message: '审核不通过'
            })
          }
          this.dialogShopState = false
          this.getList()
        }
      })
    },
    selectChange(data) {
      this.selectData = data
    },
    // 删除
    del(data) {
      this.$confirm('确认要将商品加入回收站吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delData(data)
      }).catch(() => {
        console.log('已取消')
      })
    },
    delData(data) {
      const param = {
        isDeleted: 1,
        itemId: data.itemId
      }
      commodity.upadownCommodityList(param).then(res => {
        if (res.data.code === 1) {
          this.$message({
            type: 'success',
            message: '商品已加入回收站'
          })
          this.getList()
        }
      })
    },
    // 恢复
    goodRestore(data) {
      this.$confirm('确认要将商品恢复吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.goodRestoreData(data)
      }).catch(() => {
        console.log('已取消')
      })
    },
    goodRestoreData(data) {
      const param = {
        isDeleted: 0,
        itemId: data.itemId
      }
      commodity.upadownCommodityList(param).then(res => {
        if (res.data.code === 1) {
          this.$message({
            type: 'success',
            message: '商品已恢复'
          })
          this.getList()
        }
      })
    },
    // 关联商品
    associated(data) {
      this.$confirm('确定要关联商品吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        this.$router.push({
          name: '/v1/integralgoods_add',
          params: {
            type: 4,
            data: data,
            associatedgoods: 'associatedgoods'
          }
        })
      }).catch(() => {
        console.log('已取消')
      })
    },
    // 商品分类
    goodsClass() {
      this.dialogType = 1
      this.dialogShopState = true
      const param = {
        current: 1,
        size: 3000
      }
      commodity.getCategoryList(param).then(res => {
        if (res.data.code !== 1) {
          this.$message.error(res.data.msg)
          return false
        }
        if (res.data.data === 'null' || res.data.data === null) {
          return false
        }
        // 初始化three树
        this.goodClassList = res.data.data.records
      })
    },
    threeReset() {
      var that = this
      if (that.$refs.tree) {
        that.$refs.tree.setCheckedKeys([])
      } else {
        setTimeout(function() {
          that.bindSelectedTree()
        }, 500)
      }
    },
    getCheckedNodes() {
      console.log(this.$refs.tree.getCheckedNodes())
      const threevalue = this.$refs.tree.getCheckedNodes()
      const threeArry = []
      for (const data of threevalue) {
        threeArry.push(data.label)
      }
      this.goodsClassText = threeArry.join(' | ')
      this.dialogShopState = false
    },
    // 重置
    resetFun() {
      this.$refs.seacherParam.resetFields()
      this.pageNo = 1
      this.pageSize = 10
      this.getList()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      if (this.seacherParam.itemTitle !== '' ||
        this.seacherParam.sellerItemNo !== '' ||
        this.seacherParam.listStatus !== '' ||
        this.seacherParam.censorStatus !== '') {
        this.searchStatus = true
        this.seachFun()
      } else {
        this.getList()
      }
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.pageNo = val
      if (this.seacherParam.itemTitle !== '' ||
        this.seacherParam.sellerItemNo !== '' ||
        this.seacherParam.listStatus !== '' ||
        this.seacherParam.censorStatus !== '') {
        this.searchStatus = true
        this.seachFun()
      } else {
        this.getList()
      }
    },
    shophandleClose() {
      this.dialogShopState = false
    },
    // 上下架
    listStatusVal(data) {
      if (data === 0) {
        return '待上架'
      } else if (data === 1) {
        return '已上架'
      } else if (data === 2) {
        return '已下架'
      }
    },
    // 是否新品
    isNewVal(data) {
      if (data === 1) {
        return '是'
      } else if (data === 0) {
        return '否'
      }
    },
    // 是否热卖
    isSellingHotVal(data) {
      if (data === 1) {
        return '热卖'
      } else if (data === 0) {
        return '非热卖'
      }
    },
    // 审核状态
    censorStatusVal(data) {
      if (data === 0) {
        return '待审核'
      } else if (data === 1) {
        return '已审核'
      } else if (data === 2) {
        return '审核不通过'
      }
    }
  }
}
</script>
<style type="text/scss" lang="scss" scoped>

</style>
